import { Injectable } from '@angular/core';
import { Angulartics2 } from '../../angulartics2-core';

@Injectable({ providedIn: 'root' })
export class Angulartics2IBMDigitalAnalytics {
  constructor(private angulartics2: Angulartics2) {
    if (typeof window['cmCreatePageviewTag'] !== 'function') {
      console.warn('Angulartics 2 IBM Digital Analytics Plugin: eluminate.js is not loaded');
    }
  }

  startTracking(): void {
    this.angulartics2.pageTrack
      .pipe(this.angulartics2.filterDeveloperMode())
      .subscribe(x => this.pageTrack(x.path));
    this.angulartics2.eventTrack
      .pipe(this.angulartics2.filterDeveloperMode())
      .subscribe(x => this.eventTrack(x.action, x.properties));
  }

  /**
   * Track Page in IBM Digital Analytics
   *
   * @param path location
   *
   * @link https://www.ibm.com/support/knowledgecenter/SSPG9M/Implementation/impl_pageviewtag.html
   */
  pageTrack(path: string) {
    const cmCreatePageviewTag = window['cmCreatePageviewTag'];
    cmCreatePageviewTag(path, null, null, null);
  }

  /**
   * Track an event in IBM Digital Analytics
   *
   * @param action A string corresponding to the type of event that needs to be tracked.
   * @param properties The properties that need to be logged with the event.
   */
  eventTrack(action: string, properties: any = {}) {
    const cmDisplayShops = window['cmDisplayShops'];
    switch (action) {
      /**
       * @description The Product View tag captures information about vdigitalDataiews of product detail pages.
       *  The Product View tag should be called on the lowest level detail page for products, which is typically
       *  the Product Details page. You can view example Product View tags below.
       *
       * @link https://www.ibm.com/support/knowledgecenter/SSPG9M/Implementation/impl_prodviewtag.html
       */
      case 'cmCreateProductviewTag':
        const cmCreateProductviewTag = window['cmCreateProductviewTag'];
        cmCreateProductviewTag(
          properties.productId,
          properties.productName,
          properties.categoryId,
          properties.attrbute,
          properties.virtualCategory,
        );

        break;

      /**
       * @description The Shop Action 5 tag captures data about selected products and which products are present in a shopping cart,
       *  if any, when the cart is viewed.
       *
       * @link https://www.ibm.com/support/knowledgecenter/SSPG9M/Implementation/impl_shopact5tag.html
       */
      case 'cmCreateShopAction5Tag':
        const cmCreateShopAction5Tag = window['cmCreateShopAction5Tag'];
        cmCreateShopAction5Tag(
          properties.productId,
          properties.productName,
          properties.quantity,
          properties.unitPrice,
          properties.categoryId,
          properties.attrbute,
          properties.extraFields,
          properties.virtualCategory,
        );

        cmDisplayShops();

        break;

      /**
       * @description The Shop Action 9 tag captures data about what products were purchased by a customer.
       *  Like the Shop Action 5 tag, one tag should be sent for each product line item purchased. These tags should be sent
       *  on the receipt or other completion page confirming a successful order.
       *
       * @link https://www.ibm.com/support/knowledgecenter/SSPG9M/Implementation/impl_shopact9tag.html
       */
      case 'cmCreateShopAction9Tag':
        const cmCreateShopAction9Tag = window['cmCreateShopAction9Tag'];
        cmCreateShopAction9Tag(
          properties.productId,
          properties.productName,
          properties.quantity,
          properties.unitPrice,
          properties.registrationId,
          properties.orderId,
          properties.orderSubtotal,
          properties.categoryId,
          properties.attrbute,
          properties.extraFields,
        );

        cmDisplayShops();

        break;

      /**
       * @description The Order tag captures order header information such as Registration ID, order ID, order subtotal,
       *  and shipping and handling. The Order tag should be sent on the receipt page confirming order completion.
       *
       * @link https://www.ibm.com/support/knowledgecenter/SSPG9M/Implementation/impl_ordertag.html
       */
      case 'cmCreateOrderTag':
        const cmCreateOrderTag = window['cmCreateOrderTag'];
        cmCreateOrderTag(
          properties.orderId,
          properties.orderSubtotal,
          properties.orderShipping,
          properties.registrationId,
          properties.registrantCity,
          properties.registrantState,
          properties.registrantPostalCode,
          properties.attrbute,
          properties.extraFields,
        );

        break;

      /**
       * @description The Registration tag creates a Lifetime Visitor Experience Profile (LIVE Profile) by associating a single
       *  common Registration ID with the IBM® Digital Analytics permanent cookie set in every browser visiting the tagged site.
       *
       * @link https://www.ibm.com/support/knowledgecenter/SSPG9M/Implementation/impl_registrationtag.html
       */
      case 'cmCreateRegistrationTag':
        const cmCreateRegistrationTag = window['cmCreateRegistrationTag'];
        cmCreateRegistrationTag(
          properties.registrationId,
          properties.registrantEmail,
          properties.registrantCity,
          properties.registrantState,
          properties.registrantPostalCode,
          properties.registrantCountry,
          properties.attrbute,
        );

        break;

      /**
       * @description The Element tag is used to track intra-page content in IBM® Digital Analytics. Data collected by
       *  the Element tag is used to populate values in the Element Categories and Top Viewed Elements reports.
       *
       * @link https://www.ibm.com/support/knowledgecenter/SSPG9M/Implementation/impl_elementtag.html
       */
      case 'cmCreateElementTag':
        const cmCreateElementTag = window['cmCreateElementTag'];
        cmCreateElementTag(properties.elementId, properties.elementCategory, properties.attrbute);

        break;

      /**
       * @description The Conversion Event tag is employed for tracking of general non-commerce conversion events.
       * The Conversion Event tag is used to populate values in the Conversion Events Reports and to create Key Segments.
       * This tag and the reports it populates enable analysis of a wide variety of site activities.
       *
       * @link https://www.ibm.com/support/knowledgecenter/SSPG9M/Implementation/impl_conversioneventtag.html
       */
      case 'cmCreateConversionEventTag':
        const cmCreateConversionEventTag = window['cmCreateConversionEventTag'];
        cmCreateConversionEventTag(
          properties.eventId,
          properties.actionType,
          properties.eventCategoryId,
          properties.points,
          properties.attrbute,
          properties.extraFields,
        );

        break;

      default:
        console.warn('Unsupported Event Action');
    }
  }
}
